package com.lin.controller;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class LoginInterceptor implements HandlerInterceptor {

    // 不拦截 "/login" 请求
    private static final String[] IGNORE_URI = {"/", "/login","/logout" };

    @Override
    public void afterCompletion(HttpServletRequest request,
                                HttpServletResponse response, Object handler, Exception exception)
            throws Exception {
        System.out.println("This is afterCompletion!");

    }

    @Override
    public void postHandle(HttpServletRequest request,
                           HttpServletResponse response, Object handler, ModelAndView mv)
            throws Exception {
        System.out.println("This is postHandle!");

    }

    // 该方法将在 Controller 处理前进行调用
    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response, Object handler) throws Exception {
        System.out.println("This is preHandle!");

        // flag 表示是否登录
        boolean flag = false;
        String requestURI = request.getRequestURI();
        System.out.println(requestURI);
        // 获取请求的 URL
        String url = request.getServletPath();
        System.out.println(url);
        for (String s : IGNORE_URI) {
//            if (url.contains(s)) {
//                flag = true;
//                break;
//            }
            if(url.equals(s)){
                flag = true;
                break;
            }
        }
        if (!flag) {
            // 获取 Session 并判断是否登录
            String identity = (String) request.getSession().getAttribute(HouseController.IDENTITY);
            String invoiceNo = (String) request.getSession().getAttribute(HouseController.INVOICENO);
            if (identity == null&&invoiceNo==null) {
                String contextPath = request.getContextPath();
                // 如果未登录，进行拦截，跳转到登录界面
//                request.getRequestDispatcher(contextPath+"/")
//                        .forward(request, response);
                response.sendRedirect(contextPath+"/");
            } else {
                flag = true;
            }
        }

        return flag;
    }

}
